import java.io.File;
import java.util.*;
/**
 * Call find to gen javassist style class path
 */
public class JavassistAdapter {
    public JavassistAdapter(String dir) {
        this.dir = dir;
    }

    public List<String> getResult() {
        doAdapter();
        return cp;
    }

    private void doAdapter() {
        List<String> classes = findClass();
        javassistAdapter(classes);
    }

    private List<String> findClass() {
        Find find = new Find(dir, ".class");
        find.doFind();
        return find.findResult();
    }

    private void javassistAdapter(List<String> list) {
        Iterator<String> iter = list.iterator();
        while( iter.hasNext() ) {
            String path = iter.next();
            String s = buildClassPath( path );
            if(s != null)
                cp.add( s );  
        }
    }

    private String buildClassPath(String s) {
        int loc1 = s.lastIndexOf(File.separator);
        int loc;
        while( (loc=s.indexOf('$', loc1 + 1)) != -1) {
            char c = s.charAt(loc + 1);
            if( Character.isDigit(c)) {
                return null;
            }

            loc1 = loc;
        }

        String ss = s.replace(File.separatorChar, '.');
        ss = ss.substring(0, ss.lastIndexOf('.'));  //del '.class'
        return ss;
    }

    private String dir;
    private List<String> cp = new LinkedList<String>();

    //tst begin...
    public static final void main(String []args) {

        if(args==null) {
            System.out.println("Usage: java JavassistAdapter somedir");
            return;
        }

        JavassistAdapter ja = new JavassistAdapter(args[0]);
        List<String> list = ja.getResult();

        Iterator<String> iter = list.iterator();
        while( iter.hasNext() ) {
            String path = iter.next();
            System.out.println(path);
        }
    }
    //tst end.
}
